<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <label class="el-form-item-label">名称</label>
        <el-input
          v-model="query.name"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">关键字</label>
        <el-input
          v-model="query.key"
          clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">流程类型</label>
        <el-select
          v-model="query.categoryId"
          clearable
          filterable
          size="small"
          placeholder="流程类型"
          class="filter-item"
          style="width: 200px"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryName"
            :label="item.categoryName"
            :value="item.categoryId"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <el-dialog
        :visible.sync="dialogVisible"
        width="70%"
      >
        <el-image
          style="width: 100%; height: 100%"
          :src="imageurl"
          fit="none"
        />
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="流程定义ID" />
        <el-table-column prop="rev" label="版本" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="key" label="关键字" />
        <el-table-column prop="deploymentId" label="部署ID" />
        <el-table-column label="状态" align="center" prop="suspensionState">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.suspensionState"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              :active-value="1"
              :inactive-value="2"
              @change="changeSuspensionState(scope.row, scope.row.suspensionState)"
            />
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','actReProcdef:edit']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-tooltip content="启动流程" placement="top">
              <el-button
                v-permission="['admin', 'actReProcdef:edit']"
                style="margin-left: -2px;"
                type="primary"
                size="small"
                @click="apply(scope.row)"
              ><i class="el-icon-video-play" /></el-button>
            </el-tooltip>
            <el-tooltip content="查看流程图" placement="top">
              <el-button
                v-permission="['admin', 'actReProcdef:edit']"
                style="margin-left: -2px;"
                type="primary"
                size="small"
                @click="viewImage(scope.row)"
              ><i class="el-icon-view" /></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudActReProcdef from '@/api/workflow/actReProcdef'
import crudProcessCategory from '@/api/workflow/processCategory'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, rev: null, category: null, name: null, key: null, version: null, deploymentId: null, resourceName: null, dgrmResourceName: null, description: null, hasStartFormKey: null, hasGraphicalNotation: null, suspensionState: null, tenantId: null, engineVersion: null }
export default {
  name: 'ActReProcdef',
  components: { pagination, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '流程定义管理', url: 'api/actReProcdef', idField: 'id', sort: 'id,desc', crudMethod: { ...crudActReProcdef }})
  },
  // 数据字典
  dicts: ['procdef_status'],
  data() {
    return {
      permission: {
        add: ['admin', 'actReProcdef:add'],
        edit: ['admin', 'actReProcdef:edit'],
        del: ['admin', 'actReProcdef:del']
      },
      rules: {
        id: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        key: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        version: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      imageurl: '',
      processCategoryName: '',
      // 流程类型数据
      categoryOptions: []
    }
  },
  // 页面挂载时
  mounted: function() {
    this.getCategorys()
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    /**
     * 更改流程定义状态
     */
    changeSuspensionState(data, val) {
      this.$confirm('此操作将 "' + this.dict.label.procdef_status[val] + '" ' + data.name + ', 是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudActReProcdef.edit(data).then(res => {
          this.crud.notify(this.dict.label.procdef_status[val] + '成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
        }).catch((err) => {
          console.log(err)
        })
      }).catch((err) => {
        console.log(err)
      })
    },
    /**
     * 查看流程图
     */
    viewImage(row) {
      this.dialogVisible = true
      // 读取文件流
      crudActReProcdef.viewImage(row.id).then((res) => {
        const url = window.URL.createObjectURL(res)
        this.imageurl = url
      }).catch((err) => {
        console.log(err)
      })
    },
    /**
     * 启动流程
     */
    apply(row) {
      // 启动流程参数
      const data = { pdKey: null, approveUserIds: null }
      data.pdKey = row.key
      crudActReProcdef.apply(data).then((res) => {
        this.crud.notify(row.name + '启动成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
      }).catch((err) => {
        console.log(err)
      })
    },
    /**
     * 获取流程分类数据
     */
    getCategorys() {
      crudProcessCategory.getAllCategorys().then(res => {
        this.categoryOptions = res
      }).catch(() => { })
    }
  }
}
</script>

<style scoped>
</style>
